package com.zry.fertilizer.presenter

import android.content.Context
import android.util.Log
import com.tsy.sdk.myokhttp.response.RawResponseHandler
import com.zry.fertilizer.retrofit.BaseApiRetrofit
import com.zry.fertilizer.utils.ACacheUtil
import com.zry.fertilizer.utils.ReadImgToBinary2
import com.zry.fertilizer.utils.ToastUtil
import org.json.JSONObject
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import top.zibin.luban.Luban
import java.io.File

class UploadImgPresenter(private var context: Context) {

    fun uploadFileImg( imgPath: String, OperaID: String) {
        val file1 = File(imgPath)
        if (file1.exists()) {
            Luban.get(context)
                    .load(file1)
                    .putGear(Luban.THIRD_GEAR)
                    .asObservable()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnError { throwable -> throwable.printStackTrace() }
                    .onErrorResumeNext { Observable.empty() }
                    .subscribe {
                        BaseApiRetrofit().myOkHttp.upload()
                                .url(BaseApiRetrofit.BASE_URL + "AddDetailOperaImage")
                                .addParam("OperaID", OperaID)
                                .addParam("CreateUserID", ACacheUtil().getLoginInfo().userid)
                                .addFile("image", it)




//                                .url("http://17sys.cn/service//Save?")
////                                .addParam("OperaID", OperaID)
////                                .addParam("CreateUserID", ACacheUtil().getLoginInfo().userid)
//                                .addParam("dir", OperaID)
//                                .addFile("image", it)
                                //.addFile("image", "asdsda.png", byteContents)    //直接上传File bytes
                                .tag(this)
                                .enqueue(object : RawResponseHandler() {
                                    override fun onSuccess(statusCode: Int, response: String?) {
                                        Log.e("TAGStatusCode", statusCode.toString())
                                        Log.e("TAGResponse", response.toString())
                                        val jsonObject = JSONObject(response)
                                        if (jsonObject.getString("State") == "1") {
//                                            ToastUtil.toast(context, "上传成功")
                                        } else {
                                            ToastUtil.toast(context, jsonObject.getString("Message"))
                                        }
                                    }

                                    override fun onFailure(statusCode: Int, error_msg: String?) {
                                        Log.e("TAGResponse", statusCode.toString())
                                        Log.e("TAGStatusCode", error_msg.toString())
                                    }
                                })
                    }
        }
    }


//    {"state":1,"filename":"20181021211701967.jpg","filepath":"/20181021211701967.jpg"}
//{"state":1,"filename":"20181021212013633.jpg","filepath":"d9c4504c-5eae-464a-a21e-d4dfea8c1def/20181021212013633.jpg"}


    fun uploadStringImg(what: Int, imgPath: String, OperaID: String) {
        val filePath = File(imgPath)
        if (filePath.exists()) {
            Luban.get(context)
                    .load(filePath)
                    .putGear(Luban.THIRD_GEAR)
                    .asObservable()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnError { throwable -> throwable.printStackTrace() }
                    .onErrorResumeNext { Observable.empty() }
                    .subscribe {

                        val file: File = it
                        Log.e("TAG", file.toString())

                        val params: HashMap<String, String> = hashMapOf()
                        params["OperaID"] = OperaID
                        params["CreateUserID"] = ACacheUtil().getLoginInfo().userid
                        params["ImageFile"] = ReadImgToBinary2.imgToBase64(it.toString())


                        BaseApiRetrofit().myOkHttp.post()
                                .url(BaseApiRetrofit.BASE_URL + "AddDetailOperaImage")
                                .params(params)
                                .tag(this)
                                .enqueue(object : RawResponseHandler() {
                                    override fun onSuccess(statusCode: Int, response: String?) {
                                        Log.e("TAGStatusCode", statusCode.toString())
                                        Log.e("TAGResponse", response.toString())
                                        val jsonObject = JSONObject(response)
                                        if (jsonObject.getString("state").toString() == "1") {
                                            ToastUtil.toast(context, "上传成功")
//                                showUserView.toMainActivity(what, jsonObject.getString("filepath"))
                                        } else {
                                            ToastUtil.toast(context, "上传失败")
                                        }
                                    }

                                    override fun onFailure(statusCode: Int, error_msg: String?) {
                                        Log.e("TAGResponse", statusCode.toString())
                                        Log.e("TAGStatusCode", error_msg.toString())
                                    }
                                })
                    }
        }
    }
}